package chooongg.base.utils

import android.Manifest.permission
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.provider.Settings
import androidx.annotation.RequiresPermission
import androidx.core.content.FileProvider
import chooongg.base.Utils
import java.io.File

object IntentUtils {

    /**
     * 意图是否可用
     */
    fun Intent.isAvailable() = Utils.app.packageManager.queryIntentActivities(
        this, PackageManager.MATCH_DEFAULT_ONLY
    ).size > 0

    /**
     * 安装应用意图
     */
    fun installApp(file: File): Intent {
        val intent = Intent(Intent.ACTION_VIEW)
        val type = "application/vnd.android.package-archive"
        val data = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            Uri.fromFile(file)
        } else {
            intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
            val authority = Utils.app.packageName.toString() + ".utilcode.provider"
            FileProvider.getUriForFile(Utils.app, authority, file)
        }
        intent.setDataAndType(data, type)
        return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }

    /**
     * 启动应用意图
     */
    fun launchApp(packageName: String): Intent? {
        val launcherActivity = getLauncherActivity(packageName)
        if (launcherActivity.isSpace()) return null
        val intent = Intent(Intent.ACTION_MAIN)
        intent.addCategory(Intent.CATEGORY_LAUNCHER)
        intent.setClassName(packageName, launcherActivity!!)
        return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }

    /**
     * 应用设置详情页意图
     */
    fun launchAppDetailsSettings(packageName: String): Intent {
        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.data = Uri.parse("package:${packageName}")
        return getIntent(intent, true)
    }

    /**
     * 分享文本意图
     */
    fun shareText(content: String): Intent {
        val intent = Intent(Intent.ACTION_SEND)
        intent.type = "text/plain"
        intent.putExtra(Intent.EXTRA_TEXT, content)
        return getIntent(intent, true)
    }

    /**
     * 拨号盘意图
     */
    fun dial(phoneNumber: String): Intent? {
        val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phoneNumber"))
        return getIntent(intent, true)
    }

    /**
     * 打电话意图
     */
    @RequiresPermission(permission.CALL_PHONE)
    fun call(phoneNumber: String): Intent? {
        val intent = Intent("android.intent.action.CALL", Uri.parse("tel:$phoneNumber"))
        return getIntent(intent, true)
    }

    /**
     * 发短信意图
     */
    fun sendSMS(phoneNumber: String, content: String?): Intent? {
        val intent = Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:$phoneNumber"))
        intent.putExtra("sms_body", content)
        return getIntent(intent, true)
    }

    /**
     * Return the intent of capture.
     *
     * @param outUri The uri of output.
     * @return the intent of capture
     */
    fun capture(outUri: Uri?): Intent? {
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri)
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        return getIntent(intent, true)
    }

    /**
     * 意图是否在新任务
     */
    fun getIntent(intent: Intent, isNewTask: Boolean): Intent {
        return if (isNewTask) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) else intent
    }
}